﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Logic.ClarizenSvc;
using System.Configuration;
using System.Web;


namespace Logic
{
    public class LoginComponent
    {
        public Clarizen Service {get; set;}

        

        public Boolean IsLoggedIn { get { return Service.Session != null; } }

        public void Login(String clarizenSessionID)
        {
            Clarizen c = new Clarizen();
            c.Session = new SessionHeader { ID = clarizenSessionID};
            c.EnableDecompression = true;
            Service = c;
        }

        public LoginResponse Login(string userName, string pass)
        {
            Service = new Clarizen();
            Service.Timeout = 10 * 1000 * 60; //For debugging
            Service.EnableDecompression = true; //compress traffic
            //Service.Url = "http://eptmon/BCS.API.WebService/Clarizen.svc";
            //Currently pass null in last argument
            ExtendedLoginOptions options = new ExtendedLoginOptions();

            string partnerId = "67efaea7-58a8-4339-9968-f620a4cef106";

            options.PartnerId = partnerId; //Replace with the partner id you received from Clarizen
            options.ApplicationId = "Clarizen conduit toolbar"; //Replace with a string representing your application
            options.ClientVersion = "1.0"; //Use actual version here 

            ExtendedLoginResult exResult = null;
            LoginResult lr = null;
            try
            {
                lr = Service.Login(userName, pass, options);
                exResult = lr as ExtendedLoginResult;
            }
            catch (Exception err)
            {
                LoginResponse res = new LoginResponse { ResultCode = LoginResults.Error };
                if (err.Message.StartsWith("This account has been locked."))
                    res.Message = "Your Account is locked.";
                else if (err.Message == "Username and password do not match.")
                    res.Message = "Username and password do not match.";
                else res.Message = err.Message;
                return res;
            }
            if (exResult != null)
            {
                bool mustUpgrade = exResult.ClientVersionInfo.MustUpgrade;
                string message = "Version {0} is now available at {1}\nDetails:{2}\n";
                message = mustUpgrade ?
                    message + "You must upgrade in order to continue working." :
                    message + "Would you like to upgrade?";
                message = string.Format(message, exResult.ClientVersionInfo.CurrentVersion, exResult.ClientVersionInfo.Url, exResult.ClientVersionInfo.Information);
                LoginResponse res = new LoginResponse { ResultCode = LoginResults.Update, Message = message, LoginResult=lr };
                return res;
            }

            return new LoginResponse { ResultCode = LoginResults.Success, Message = "O.K.", LoginResult = lr};
        }

        public class LoginResponse
        {
            public String Message;
            public LoginResults ResultCode;
            public LoginResult LoginResult;
        }

        public enum LoginResults
        {
            Success,
            Error,
            Update,
        }
    }
}
